declare
fun {NewMonitor}
   Q={NewQueue}
   L={NewGRLock}

   proc {LockM P}
      if {L.get} then
         try {P} finally {L.release} end
      else {P} end
   end

   proc {WaitM}
   X in
      {Q.insert X} {L.release} {Wait X}
      if {L.get} then skip end
   end

   proc {NotifyM}
   U={Q.deleteNonBlock} in
      case U of [X] then X=unit else skip end
   end

   proc {NotifyAllM}
   L={Q.deleteAll} in
      for X in L do X=unit end
   end
in
   monitor('lock':LockM wait:WaitM notify:NotifyM
           notifyAll:NotifyAllM)
end
